package com.rongji.dfish.demo.login.view;

import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.framework.FrameworkHelper;
import com.rongji.dfish.ui.Widget;
import com.rongji.dfish.ui.form.AbstractInput;
import com.rongji.dfish.ui.form.Password;
import com.rongji.dfish.ui.form.Text;
import com.rongji.dfish.ui.form.Validate;
import com.rongji.dfish.ui.helper.FormPanel;
import com.rongji.dfish.ui.layout.ButtonBar;
import com.rongji.dfish.ui.layout.HorizontalLayout;
import com.rongji.dfish.ui.layout.VerticalLayout;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.Button;
import com.rongji.dfish.ui.widget.Html;

public class LoginView {
	public static View buildLoginView() {
		View view =buildDialogView(350);
		FormPanel formPanel = (FormPanel) view.findNodeById("form");
		VerticalLayout form=new VerticalLayout("vert");
		form.add(getTextImgSet(new Text("loginName", "用户名", "DFish_admin").setId("loginName").setBeforecontent("<i class='log-input-loginName'></i>")));
		form.add(getTextImgSet(new Password("password", "密码", "DFish3@0315").setId("password").setBeforecontent("<i class='log-input-password'></i>").setAutocomplete(true)));
		formPanel.add(form);
		
		form.add(new Html("").setHeight(20));
		form.add(new ButtonBar("btnLogin").add(new Button("登录").setCls("log-btn").setOn(Button.EVENT_CLICK, "dfish.vm(this).cmd('login');")).setAlign(ButtonBar.ALIGN_CENTER).setHeight(55));
		form.add(new Html("<a onclick=\"dfish.vm(this).cmd('toReg');\" >免费注册</a>").setHeight(20).setCls("log-link-right"));
		view.addCommand("login", PubView.getSubmitCommand("login/doLogin"));
		view.addCommand("toReg", PubView.getAjaxCommand("login/toReg"));
		view.addCommand("showAgree", PubView.getAjaxCommand("login/showAgree"));
		view.addCommand("doReg", PubView.getSubmitCommand("login/doReg"));
		view.addCommand("changeToLogin", PubView.getAjaxCommand("login/changeToLogin"));
		return view;
	}

	public static VerticalLayout buildToRegView(int inputHeight) {
		View view =buildDialogView(inputHeight);
//		VerticalLayout form = (VerticalLayout) view.findNodeById("form");
//		
//		form.add(getTextSet(new Text("loginName", "账号", null).setId("loginName").setBeforecontent("<i class='log-input-loginName'></i>")));
//		form.add(getTextSet(new Text("userName", "姓名", null).setBeforecontent("<i class='log-input-userName'></i>")));
//		form.add(getTextSet(new Password("password", "密码", null).setBeforecontent("<i class='log-input-password'></i>").setAutocomplete(true)));
//		form.add(getTextSet(new Password("cfmPassword", "确认密码", null).setBeforecontent("<i class='log-input-password'></i>").setAutocomplete(true)
//				.setOn(Password.EVENT_BLUR, "login.checkPwd(this);")));
		FormPanel form = (FormPanel) view.findNodeById("form");
		form.add(getTextSet(new Text("loginName", "账号", null).setId("loginName")));
		form.add(getTextSet(new Text("userName", "姓名", null)));
		form.add(getTextSet(new Text("userEmail", "邮箱", null)).setRequired(true)
				.addValidate(new Validate().setPattern("/^[a-zA-Z0-9_-]+@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}/")));
		form.add(getTextSet(new Password("password", "密码", null).setAutocomplete(true)));
		form.add(getTextSet(new Password("cfmPassword", "确认密码", null).setAutocomplete(true)
				.setOn(Password.EVENT_BLUR, "login.checkPwd(this);")));
		
		form.add(new Html("点击「注册」按钮，即代表你同意<a onclick=\"dfish.vm(this).cmd('showAgree');\" >《DFish开发框架使用协议》</a>").setHeight(20).setStyle("color: #999;text-align:center"));
		form.add(new ButtonBar("btnLogin").add(new Button("注册").setCls("log-btn").setOn(Button.EVENT_CLICK, "login.checkPwd(this,'1')")).setAlign(ButtonBar.ALIGN_CENTER).setHeight(55));
		form.add(new Html("<a onclick=\"dfish.vm(this).cmd('changeToLogin');\" >已有账号？登录</a>").setHeight(20).setCls("log-link-right"));
		return (VerticalLayout) view.findNodeById("main");
	}
	
	private static AbstractInput<?,?> getTextImgSet(AbstractInput<?,?> input){
		String enterKey = "login.enterKey(this);";
		input.setPlaceholder("请输入"+input.getLabel()).setRequired(true);
		input.setCls("log-input");
		input.setTransparent(true).setWmin(110).setOn(Text.EVENT_INPUT, "this.fixhdr()").setOn(Text.EVENT_KEYUP, enterKey).setHeight(55);
		return input;
	}
	private static AbstractInput<?,?> getTextSet(AbstractInput<?,?> input){
		String enterKey = "login.enterKey(this);";
		input.setPlaceholder("请输入"+input.getLabel()).setRequired(true);
		input.setOn(Text.EVENT_INPUT, "this.fixhdr()").setOn(Text.EVENT_KEYUP, enterKey).setWidth(260);
		return input;
	}
	
	public static View buildDialogView(int inputHeight){
		View view = new View();
		
		VerticalLayout root = new VerticalLayout("root");
		view.setNode(root);
		view.setOn(View.EVENT_LOAD, "var $=dfish;if($.br.scroll)return;" +
				"var m=this,o=m.f('loginName'),t=!o.x.value&&setInterval(function(){" +
				"if(o.val()){$.classAdd(o.$('ph'),'f-none');" +
				"$.classAdd(m.f('password').$('ph'),'f-none');clearInterval(t)}},50);" +
				"$.query(document).on('click',function(){clearInterval(t)})");
		HorizontalLayout top = new HorizontalLayout("top").setStyle("background-color:#2498ff;");
		root.add(top, "130");
		
		VerticalLayout topShell = new VerticalLayout("topShell").setCls("ico-logo");
		top.add(topShell, "*");
		
		VerticalLayout main = new VerticalLayout("main").setAlign(VerticalLayout.ALIGN_CENTER).setValign(VerticalLayout.VALIGN_MIDDLE);
		root.add(main, "*");
		
		FormPanel form = new FormPanel("form");
//		VerticalLayout form = new VerticalLayout("form");
		main.add(form.setHeight(inputHeight));
		form.setScroll(true);
		form.setStyle("padding:35px 0;").setHmin(70).setWidth(450).setHeight(600);
		
		return view;
	}

	public static Widget<?> buildAgreeView() {
		View view = PubView.buildDialogView();
		
		VerticalLayout main = new VerticalLayout(PubView.DIALOG_MAIN).setScroll(true);
		view.replaceNodeById(main);
		Html agreeList=new Html(FrameworkHelper.getFileText("m/login/html/agree.htm")).setScroll(true);
		main.add(agreeList);
		
		ButtonBar btnBar = (ButtonBar) view.findNodeById(PubView.DIALOG_BTN);
		return view;
	}
	
}
